#!/usr/bin/env python
# -*- coding: utf-8 -*-

from app import app
from database import db
from models import User
from werkzeug.security import generate_password_hash, check_password_hash

# 查看和重置用户密码
with app.app_context():
    # 查看所有用户
    users = User.query.all()
    print(f"数据库中共有 {len(users)} 个用户：")
    for user in users:
        print(f"- 用户名: {user.username}, 邮箱: {user.email}, 管理员: {user.is_admin}")
        
    # 重置admin用户密码为admin123
    admin = User.query.filter_by(username='admin').first()
    if admin:
        admin.password = generate_password_hash('admin123', method='sha256')
        db.session.commit()
        print("\n已成功将admin用户密码重置为: admin123")
        
    # 可选：创建或重置其他测试用户
    test_users = [
        {'username': 'developer1', 'password': 'password1'},
        {'username': 'developer2', 'password': 'password2'},
        {'username': 'tester1', 'password': 'password3'},
        {'username': 'projectmanager', 'password': 'password4'}
    ]
    
    for user_info in test_users:
        user = User.query.filter_by(username=user_info['username']).first()
        if user:
            user.password = generate_password_hash(user_info['password'], method='sha256')
            print(f"已重置用户 {user_info['username']} 的密码为: {user_info['password']}")
        else:
            new_user = User(
                username=user_info['username'],
                email=f"{user_info['username']}@example.com",
                password=generate_password_hash(user_info['password'], method='sha256')
            )
            db.session.add(new_user)
            print(f"已创建新用户 {user_info['username']}，密码为: {user_info['password']}")
    
    db.session.commit()
    print("\n操作完成，请尝试使用重置后的密码登录。")